<html>
<head><meta charset="utf-8"><title>Generating debug information for &amp;str arguments · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Generating.20debug.20information.20for.20.26str.20arguments.html">Generating debug information for &amp;str arguments</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224916094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Generating%20debug%20information%20for%20%26str%20arguments/near/224916094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nam Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Generating.20debug.20information.20for.20.26str.20arguments.html#224916094">(Feb 02 2021 at 18:38)</a>:</h4>
<p>Hi everyone,<br>
Recently, when I tried to debug a Rust binary with WinDbg, I realized that the visualization for objects of type &amp;str that are passed as function arguments are incorrect. For instance, with this program:<br>
&nbsp;</p>
<div class="codehilite"><pre><span></span><code>#[inline(never)]
pub fn nam_foo_function(first_var: &amp;str) -&gt; usize {
    first_var.len()
}

fn main() {
    let s = &quot;hello&quot;;
    let input = &amp;s[1..3];
    nam_foo_function(input);
}
</code></pre></div>
<p>I got this debug information <a href="/user_uploads/4715/xXXZEHLP79vdozNSnBAkNTRA/image.png">(length should be 5 and the address that data_ptr points to contains no ASCII character) </a></p>
<div class="message_inline_image"><a href="/user_uploads/4715/xXXZEHLP79vdozNSnBAkNTRA/image.png" title="(length should be 5 and the address that data_ptr points to contains no ASCII character) "><img src="/user_uploads/4715/xXXZEHLP79vdozNSnBAkNTRA/image.png"></a></div><p>I would like to do something to address this problem. A little investigation showed me that Rust breaks down every &amp;str or slice argument into two arguments, a pointer and an int. However, in the LLVM IR, there is only debug information for the original &amp;str or slice argument. Here is a segment of the LLVM IR for the program I posted above:<br>
&nbsp;</p>
<div class="codehilite"><pre><span></span><code>; main::nam_foo_function
; Function Attrs: noinline uwtable
define internal i64 @_ZN4main16nam_foo_function17h6e11569add74fc3fE([0 x i8]* noalias nonnull readonly align 1 %first_var.0, i64 %first_var.1) unnamed_addr #0 !dbg !408 {
start:
  %first_var.dbg.spill = alloca { [0 x i8]*, i64 }, align 8
  %0 = getelementptr inbounds { [0 x i8]*, i64 }, { [0 x i8]*, i64 }* %first_var.dbg.spill, i32 0, i32 0
  store [0 x i8]* %first_var.0, [0 x i8]** %0, align 8
  %1 = getelementptr inbounds { [0 x i8]*, i64 }, { [0 x i8]*, i64 }* %first_var.dbg.spill, i32 0, i32 1
  store i64 %first_var.1, i64* %1, align 8
  call void @llvm.dbg.declare(metadata { [0 x i8]*, i64 }* %first_var.dbg.spill, metadata !412, metadata !DIExpression()), !dbg !413
; call core::str::&lt;impl str&gt;::len
  %2 = call i64 @&quot;_ZN4core3str21_$LT$impl$u20$str$GT$3len17ha405126c3a923becE&quot;([0 x i8]* noalias nonnull readonly align 1 %first_var.0, i64 %first_var.1), !dbg !414
  br label %bb1, !dbg !414
</code></pre></div>
<p>&nbsp;<br>
I plan to generate the debug information for <code>first_var.0</code> and <code>first_var.1</code> to see if that would fix the issue.  I had a look at the function <code>debug_introduce_local</code> at <code>compiler\rustc_codegen_ssa\src\mir\debuginfo.rs</code> and realized that these two arguments are not treated as locals, and it seems that Rust compiler does not generate debug information for values that are not local refs. I wonder if my understanding of how debug information for &amp;str and slice arguments is generated is correct. If so, is there any way to force Rust to emit debug information for components of &amp;str and slice arguments, even though they are not locals?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>